package cmd

import (
	"bufio"
	"flag"
	"fmt"
	"os"
	"strings"
	"time"

	"cli/nets"
	"cli/util"
)

func BuyCard() {
	buyCard := flag.NewFlagSet("buy_card", flag.ExitOnError)
	var isGirl bool
	var times int
	var month int
	var day int
	var year int
	var price int
	var inviteTeacherId int
	var userName string
	var cardName string

	buyCard.BoolVar(&isGirl, "girl", false, "女生因为生理期,购买时间卡使用这个可以每月多5天")
	buyCard.IntVar(&times, "times", 0, "会员卡可用次数")
	buyCard.IntVar(&month, "month", 0, "会员卡可用月数")
	buyCard.IntVar(&day, "day", 0, "会员卡可用天数")
	buyCard.IntVar(&year, "year", 0, "会员卡可用年数")
	buyCard.IntVar(&price, "price", 0, "用户购买的价格(实际支付)")
	buyCard.IntVar(&inviteTeacherId, "invite_teacher_id", -1, "哪个老师邀请的，这个可以方便的算出提成等信息,也可以忽略不用")
	buyCard.StringVar(&userName, "user_name", "", "用户的名称")
	buyCard.StringVar(&cardName, "card_name", "", "会员卡的名称")

	buyCard.Parse(os.Args[2:])

	if times == 0 && month == 0 && day == 0 && year == 0 {
		fmt.Println("如果这是时效卡,请一定输入正确的卡使用次数,月数,天数,或者年数,如果不是，请忽略")
	}
	var endDate time.Time
	if isGirl {
		fmt.Println("选择为女生优惠,购买时间卡使用这个可以每月多5天")
		endDate = util.GirlGetEndDate(year, month, day)
	} else {
		endDate = util.GetEndDate(year, month, day)
	}
	if times != 0 {
		fmt.Println("这张卡能使用 : ", times, " 次")
	}
	if month != 0 || day != 0 || year != 0 {
		fmt.Println("这张卡将直到 : ", endDate.Format("2006-01-02"), " 一直有效")
	}
	if inviteTeacherId != -1 {
		fmt.Println("是id为 : ", inviteTeacherId, " 的这个老师邀请的")
	}
	fmt.Println("收入: ", price, "用户名字为: ", userName, "会员卡名称为: ", cardName)
	fmt.Println("以上是这次购买的详细信息,请你确认要给用户这张会员卡,按\"y\"并按下回车(enter)确认执行操作,按\"n\"其他按键并按下回车(enter)取消执行")
	reader := bufio.NewReader(os.Stdin)
	input, _ := reader.ReadString('\n')
	input = strings.ToLower(strings.TrimSpace(input))
	if input == "y" {
		fmt.Println("购买将执行")
		nets.BuyCardByName(endDate, times, inviteTeacherId, price, userName, cardName)
	} else {
		fmt.Println("取消购买")
	}

}
func SelectAllCardInfo() {
	selectCard := flag.NewFlagSet("select_cards", flag.ExitOnError)
	selectCard.Parse(os.Args[2:])
	nets.SelectAllCardInfo()
}
